s = '{name} has {n} messages'
s.format(name='Guido', n=37)

name = 'Guido'
n = 37
s.format_map(vars())


class Info:
    def __init__(self, name, n):
        self.name = name
        self.n = n


a = Info('Guido', 37)
s.format_map(vars(a))


class Safesub(dict):
    """防止key找不到"""

    def __missing__(self, key):
        return '{' + key + '}'


del n
s.format_map(Safesub(vars()))

import sys


def sub(text):
    return text.format_map(Safesub(sys._getframe(1).f_locals))

name = 'Guido'
n = 37
print(sub('Hello {name}'))

print(sub('You have {n} messages.'))

print(sub('Your favorite color is {color}'))